<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

//商品列表
class Proview extends Base
{
	//查看商品名称下商品列表
	public function good_list()
	{

		$data = $this->request->param();
		$this->assign('gid', isset($data['gid']) ? $data['gid'] : 0);
		$this->assign('tag_id', isset($data['tag_id']) ? $data['tag_id'] : 0);
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$map = [];
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);
			$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'updatetime';
			$order = isset($_POST['order']) ? ' ' . strval($_POST['order']) : ' desc';
			//查询start
			$one = ''; //关联序列号查询




			if (isset($data['search']) && !empty($data['search'])) {
				$data['search'] = trim($data['search']);
				if ($data['tj'] == 0) {
					$f = Db::name('tagslogs')->where('sn', $data['search'])->find();
					if (!empty($f) && $f['snkey'] == 1) {
						$msn = more_sn($data['search']);
						if ($msn['status'] == 1) {
							$one = $msn['one'];
						}
					}
					$map['sn'] = ['like', '%' . trim($data['search']) . '%'];
					//['like','%'.trim($data['username']).'%']
				} elseif ($data['tj'] == 1) {
					$gidarr = Db::name('good')->where('name', 'like', '%' . trim($data['search']) . '%')->distinct(true)->field('id')->select();

					if (!empty($gidarr)) {

						$gidarr1 = array_to_arr($gidarr, 'id');
						$tagidarr = Db::name('goodstags')->where('good_id', 'in', $gidarr1)->distinct(true)->field('id')->select();
						if (!empty($tagidarr)) {
							$tagidarr1 = array_to_arr($tagidarr, 'id');
							$map['goodstags_id'] = ['in', $tagidarr1];
						}
					}
				}
			}
			
			//halt($map);
			//时间查询
			if (!empty($data['stime']) && !empty($data['etime'])) {
				$day_start = strtotime($data['stime'] . "0:0:0"); //日初
				$day_end = strtotime($data['etime'] . "23:59:59"); //日末

				$map['addtime'] = ['between', [$day_start, $day_end]];
			}



			//查询end


			if ($data['tag_id'] == 0) {
				//gid-商品ID
				$gid = $data['gid'];
				$g = Db::name('good')->where('id', $gid)->field('id,name,model')->find();
				$g['gys_name'] = Db::name('supplier')->where('id', $g['supplier_id'])->value('name');
				//halt($g);
				//tagid-商品ID下标签ID
				$tagidarr = Db::name('goodstags')->where('good_id', $gid)->distinct(true)->field('id')->select();
				if ($gid != 0 && empty($tagidarr)) {
					$count = 0;
					$list = [];
					return $this->json_data($count, $list);
				}
				$tagid = array_to_arr($tagidarr, 'id');
				if ($map == []) {

					if ($gid == 0) {
						$list = Db::name('tagslogs')->where('sn', 'neq', '')->where('snkey', 0)->order($sort . $order)->limit($start, $limit)->select();
						$count = Db::name('tagslogs')->where('sn', 'neq', '')->where('snkey', 0)->order('updatetime desc')->count();
						// $list = Db::name('tagslogs')->where('sn', 'neq', '')->order($sort . $order)->select();
						// $list=more_sn_one($list,'sn');
						// $count=count($list);
						// $list=array_slice($list,$start,$limit,false);//分页
					} else {
						$list = Db::name('tagslogs')->where('goodstags_id', 'in', $tagid)->where('snkey', 0)->where('sn', 'neq', '')->order($sort . $order)->limit($start, $limit)->select();
						$count = Db::name('tagslogs')->where('goodstags_id', 'in', $tagid)->where('snkey', 0)->where('sn', 'neq', '')->order('updatetime desc')->count();
						// $list = Db::name('tagslogs')->where('goodstags_id', 'in', $tagid)->where('sn', 'neq', '')->order($sort . $order)->select();
						// $list=more_sn_one($list,'sn');
						// $count=count($list);
						// $list=array_slice($list,$start,$limit,false);//分页
					}
				} else {
					//halt($map);

					if ($gid == 0) {

						if (empty($one)) {

							$list = Db::name('tagslogs')->where($map)->where('snkey', 0)->where('sn', 'neq', '')->order($sort . $order)->limit($start, $limit)->select();
							// echo '<pre>';
							// var_dump($list);
							// exit;
						} else {
							// $list = Db::name('tagslogs')->where($map)->where('snkey',0)->where('sn', 'neq', '')->union(function($query)use($one){
							// 	$query->name('tagslogs')->where('sn',$one);
							// })->order($sort . $order)->limit($start, $limit)->select();

							$list = Db::name('tagslogs')->where($map)->where('snkey', 0)->where('sn', 'neq', '')->union(function ($query) use ($one) {
								$query->name('tagslogs')->where('sn', $one);
							})->order($sort . $order)->limit($start, $limit)->select();
							$list = Db::name('tagslogs')->where('sn', $one)->select();
						}

						$count = Db::name('tagslogs')->where($map)->where('snkey', 0)->where('sn', 'neq', '')->order('updatetime desc')->count();

						// $list = Db::name('tagslogs')->where($map)->where('sn', 'neq', '')->order($sort . $order)->select();
						// $list=more_sn_one($list,'sn');
						// $count=count($list);
						// $list=array_slice($list,$start,$limit,false);//分页
					} else {

						if (empty($one)) {
							$list = Db::name('tagslogs')->where('goodstags_id', 'in', $tagid)->where('snkey', 0)->where($map)->where('sn', 'neq', '')->order($sort . $order)->limit($start, $limit)->select();
						} else {
							$list = Db::name('tagslogs')->where('goodstags_id', 'in', $tagid)->where('snkey', 0)->where($map)->where('sn', 'neq', '')->union(function ($query) use ($one) {
								$query->name('tagslogs')->where('sn', $one);
							})->order($sort . $order)->limit($start, $limit)->select();
						}


						$count = Db::name('tagslogs')->where('goodstags_id', 'in', $tagid)->where('snkey', 0)->where($map)->where('sn', 'neq', '')->order('updatetime desc')->count();

						// $list =  Db::name('tagslogs')->where('goodstags_id', 'in', $tagid)->where($map)->where('sn', 'neq', '')->order($sort . $order)->select();
						// $list=more_sn_one($list,'sn');
						// $count=count($list);
						// $list=array_slice($list,$start,$limit,false);//分页
					}
				}
			} else {
				$tagid = $data['tag_id'];
				if ($map == []) {

					//$list = Db::name('tagslogs')->where('goodstags_id', $tagid)->order('updatetime desc')->limit($start, $limit)->select();
					$list = Db::name('tagslogs')->where('goodstags_id', $tagid)->order($sort . $order)->limit($start, $limit)->select();
					$count = Db::name('tagslogs')->where('goodstags_id', $tagid)->order('updatetime desc')->count();
				} else {



					if (empty($one)) {
						$list = Db::name('tagslogs')->where('goodstags_id', $tagid)->where($map)->order($sort . $order)->limit($start, $limit)->select();
					} else {
						$list = Db::name('tagslogs')->where('goodstags_id', $tagid)->where($map)->union(function ($query) use ($one) {
							$query->name('tagslogs')->where('sn', $one);
						})->order($sort . $order)->limit($start, $limit)->select();
					}
					$count = Db::name('tagslogs')->where('goodstags_id', $tagid)->where($map)->order('updatetime desc')->count();
				}
			}
			//halt($tagid);
			//获取属与tagid下的商品序列号列表
			//halt($tagid);


			foreach ($list as $key => &$val) {
				$val['addtime'] = isset($val['addtime']) && $val['addtime'] != 0 ? date('Y-m-d', $val['addtime']) : '';
				$val['name'] = $this->getgxx($val['goodstags_id'], 'name');
				$val['model'] = $this->getgxx($val['goodstags_id'], 'model');
				$val['gys_name'] = $this->getgxx($val['goodstags_id'], 'gys_name');
				$val['outtime'] = isset($val['outtime']) && $val['outtime'] != 0 ? date('Y-m-d', $val['outtime']) : '';
				$val['wxjl'] = $this->getwxjl($val['sn']); //维修记录
				$val['wxjl_id'] = $this->getwxjl_id($val['sn']); //维修记录id
				$val['bxjh'] = $this->getbxjh($val['sn'])['checkstatus_name']; //保修激活
				$val['jhsh'] = $this->getbxjh($val['sn'])['jhsh']; //激活审核
				$val['bxjh_id'] = $this->getbxjh_id($val['sn']); //保修卡id
				$val['user'] = $this->getkh($val['sn'], 'user'); //客户姓名
				$val['mobile_phone'] = $this->getkh($val['sn'], 'mobile_phone'); //客户姓名
				$val['phone'] = $this->getkh($val['sn'], 'phone'); //客户姓名
				$val['danwei'] = $this->getkh($val['sn'], 'danwei'); //客户姓名
				$val['attr_sn'] = more_sn($val['sn'])['sn'];
				// 获取标签主表信息
				$goodstags = Db::name('goodstags')->where('id', $val['goodstags_id'])->find();
				// 获取商品信息
				$good = [];
				if (!empty($goodstags)) {
					$good = Db::name('good')->where('id', $goodstags['good_id'])->find();
				}
				// 查询分类
				$series = [];
				if (!empty($good)) {
					$series = Db::name('series')->where('id', $good['series'])->find();
					$val['series'] = $series['name'];
				}
				// 查询品牌
				$brand = [];
				if (!empty($series)) {
					$brand = Db::name('brand')->where('id', $series['brand_id'])->find();
					$val['brand'] = $brand['name'];
				}
				// 查询公司
				$company = [];
				if (!empty($brand)) {
					$company = Db::name('company')->where('id', $brand['company_id'])->find();
					$val['company'] = $company['name'];
				}
			}
			//halt($list);
			return $this->json_data($count, $list);
		}
		return $this->fetch('good_list');
	}

	public function daochu()
	{
		// 组装数据
		$data = $this->request->param();
		if (isset($data['stime']) && !empty($data['stime'])) {
			$data['stime'] = strtotime($data['stime'] . ' 00:00:00');
			$data['etime'] = strtotime($data['etime'] . ' 23:59:59');
			$lists = Db::name('tagslogs')->where('sn', '<>', '')->where('addtime', '>=', $data['stime'])->where('addtime', '<=', $data['etime'])->order('id desc')->select();
		} else {
			$lists = Db::name('tagslogs')->where('sn', '<>', '')->order('id desc')->select();
		}
		foreach ($lists as $key => $value) {
			$good = Db::name('good')->where('id', Db::name('goodstags')->where('id', $value['goodstags_id'])->find()['good_id'])->find();
			$good['gys_name'] = Db::name('supplier')->where('id', $good['supplier_id'])->value('name');
			$lists[$key]['good_name'] = $good['name'];
			$lists[$key]['good_model'] = $good['model'];
			$lists[$key]['good_gys_name'] = $good['gys_name'];
			$lists[$key]['fix_count'] = Db::name('fix')->where('tagslogs_sn', $value['sn'])->find()['fixnum'] ?: '无记录';
			$card = Db::name('card')->where('tagslogs_sn', $value['sn'])->find();
			$lists[$key]['card_user'] = '';
			$lists[$key]['card_phone'] = '';
			$lists[$key]['card_mobile_phone'] = '';
			$lists[$key]['card_danwei'] = '';
			if ($card) {
				switch ($card['checkstatus']) {
					case 0:
						$lists[$key]['checkstatus_name'] = '待审核';
						break;
					case 1:
						$lists[$key]['checkstatus_name'] = '通过';
						break;
					case 2:
						$lists[$key]['checkstatus_name'] = '拒绝';
						break;
					default:
						# code...
						break;
				}
				$lists[$key]['card_user'] = $card['user'];
				$lists[$key]['card_phone'] = $card['phone'];
				$lists[$key]['card_mobile_phone'] = $card['mobile_phone'];
				$lists[$key]['card_danwei'] = $card['danwei'];
			} else {
				$lists[$key]['checkstatus_name'] = '未激活';
			}
			$lists[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s', $value['addtime']) : '';
			$lists[$key]['outtime'] = $value['outtime'] ? date('Y-m-d H:i:s', $value['outtime']) : '';
		}

		// 导出
		$spreadsheet = new Spreadsheet();
		$PHPSheet = $spreadsheet->getActiveSheet();

		$PHPSheet->setTitle("商品列表"); //给当前活动sheet设置名称
		$PHPSheet->setCellValue("A1", "商品名称")
			->setCellValue("B1", "商品型号")
			->setCellValue("C1", "商品序列号")
			->setCellValue("D1", "入库时间")
			->setCellValue("E1", "出库时间")
			->setCellValue("F1", "维修记录")
			->setCellValue("G1", "保修激活")
			->setCellValue("H1", "供应商名称")
			->setCellValue("I1", "客户姓名")
			->setCellValue("J1", "手机")
			->setCellValue("K1", "固话")
			->setCellValue("L1", "单位名称");

		$styleArray = [
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
		];
		foreach ($lists as $key => $val) {
			$spreadsheet->setActiveSheetIndex(0)
				->setCellValue("A" . ($key + 2), $val['good_name'])
				->setCellValue("B" . ($key + 2), $val['good_model'])
				->setCellValue("C" . ($key + 2), $val['sn'])
				->setCellValue("D" . ($key + 2), $val['addtime'])
				->setCellValue("E" . ($key + 2), $val['outtime'])
				->setCellValue("F" . ($key + 2), $val['fix_count'])
				->setCellValue("G" . ($key + 2), $val['checkstatus_name'])
				->setCellValue("H" . ($key + 2), $val['good_gys_name'])
				->setCellValue("I" . ($key + 2), $val['card_user'])
				->setCellValue("J" . ($key + 2), $val['card_mobile_phone'])
				->setCellValue("K" . ($key + 2), $val['card_phone'])
				->setCellValue("L" . ($key + 2), $val['card_danwei']);
			$spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
		}

		$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(20);
		$spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(20);
		$spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(15);
		$spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(15);

		$time = date('Y-m-d', time());
		// Redirect output to a client’s web browser (Xlsx)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="商品列表' . $time . '.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0

		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
		$writer->save('php://output');
	}

	//获取商品信息-品牌、规格、供应商名称
	public function getkh($sn, $value)
	{
		$info = Db::name('card')->where('tagslogs_sn', $sn)->find();
		// if (empty($info)) {
		// 	$info = Db::name('card')->where('tagslogs_sn', 'like', '%' . $sn . '%')->find();
		// }
		$gxx = $info[$value];
		return $gxx;
	}
	//获取商品信息-品牌、规格、供应商名称
	public function getgxx($id, $value)
	{
		$gid = Db::name('goodstags')->where('id', $id)->value('good_id');
		if ($value == 'gys_name') {
			$good = Db::name('good')->where('id', $gid)->find();
			$gxx = Db::name('supplier')->where('id', $good['supplier_id'])->value('name');
		} else {
			$gxx = Db::name('good')->where('id', $gid)->value($value);
		}
		return $gxx;
	}

	//获取保修激活//激活审核
	public function getbxjh($sn)
	{
		if (empty($sn)) {
			$info_a['checkstatus_name'] = '未激活';
			$info_a['jhsh'] = 0;
			return $info_a;
		}
		$info = Db::name('card')->where('tagslogs_sn', $sn)->find();
		if (empty($info)) {
			$info = Db::name('card')->where('tagslogs_sn', 'like', '%' . $sn . '%')->find();
		}
		$info_a = [];
		$info_a['jhsh'] = 0;
		//halt($info);

		if (empty($info)) {
			$info_a['checkstatus_name'] = '未激活';
		} else {


			if ($info['checkstatus'] == 0) {
				$info_a['jhsh'] = 1;
				$info_a['checkstatus_name'] = '待审核';
			} elseif ($info['checkstatus'] == 1) {
				$info_a['jhsh'] = 2;
				$info_a['checkstatus_name'] = '通过';
				//halt($info_a);
			} else {
				$info_a['checkstatus_name'] = '拒绝';
			}

			// if(empty($info['sn'])){
			// 	$info_a['jhsh']=0;
			// 	$info_a['checkstatus_name']='未激活';
			// }else{

			// }


		}

		//halt($info_a);
		return $info_a;
	}
	//获取保修卡ID
	public function getbxjh_id($sn)
	{
		$info = Db::name('card')->where('tagslogs_sn', $sn)->find();
		if (empty($info)) {
			$info = Db::name('card')->where('tagslogs_sn', 'like', '%' . $sn . '%')->find();
		}


		return $info['id'];
	}

	//获取维修记录
	public function getwxjl($sn)
	{
		$fix = Db::name('fix')->where('tagslogs_sn', $sn)->find();
		if (empty($fix)) {
			$fix = Db::name('fix')->where('tagslogs_sn', 'like', '%' . $sn . '%')->find();
		}
		if (empty($fix)) {
			$fix['fixnum'] = 0;
		}
		return $fix['fixnum'];
	}

	//获取维修记录id
	public function getwxjl_id($sn)
	{
		$fix = Db::name('fix')->where('tagslogs_sn', $sn)->find();
		if (empty($fix)) {
			$fix = Db::name('fix')->where('tagslogs_sn', 'like', '%' . $sn . '%')->find();
		}

		return $fix['id'];
	}
	//编辑1-1
	public function edit($id)
	{
		$info = Db::name('tagslogs')->where('id', $id)->find();
		$info['addtime'] = isset($info['addtime']) && $info['addtime'] != 0 ? date('Y-m-d H:i:s', $info['addtime']) : '';
		$info['outtime'] = isset($info['outtime']) && $info['outtime'] != 0 ? date('Y-m-d H:i:s', $info['outtime']) : '';
		$this->assign('info', $info);
		return $this->fetch('edit');
	}

	//编辑1-2
	public function save()
	{
		$status = 0;
		$msg = "操作失败";
		$data = $this->request->param();
		//执行更新操作
		$id = $data['id'];
		$data['sn'] = trim($data['sn']);
		$data['addtime'] = isset($data['addtime']) && $data['addtime'] != 0 ? strtotime($data['addtime']) : '';
		$data['outtime'] = isset($data['outtime']) && $data['outtime'] != 0 ? strtotime($data['outtime']) : '';
		unset($data['id']);
		//halt($data);
		$sn = Db::name('tagslogs')->where('id', $id)->find()['sn'];
		$res = Db::name('tagslogs')->where('id', $id)->update($data);
		//fix、card、more_sn
		Db::name('fix')->where('tagslogs_sn', $sn)->update(['tagslogs_sn' => $data['sn']]);
		Db::name('card')->where('tagslogs_sn', $sn)->update(['tagslogs_sn' => $data['sn']]);
		if ($sn) {
			$more_sn = Db::name('more_sn')->where('key', 'like', '%' . $sn  . '%')->find();
			if ($more_sn) {
				$arr = explode(',', $more_sn['key']);
				unset($arr[array_search($sn, $arr)]);
				array_push($arr, $data['sn']);
				$key = implode(',', $arr);
				Db::name('more_sn')->where('id', $more_sn['id'])->update(['key' => $key]);
			}
		}

		if ($res) {
			$status = 1;
			$msg = "更新成功";
			adminlog('修改商品序列号:' . $data['sn'], '修改');
		}
		return ['status' => $status, 'msg' => $msg];
	}

	// 关联序列号1-1
	public function moresn($id)
	{
		$info = Db::name('tagslogs')->where('id', $id)->find();
		$arr = [];
		$more_sn = more_sn($info['sn']);
		if ($more_sn['status'] == 1) {
			$info = Db::name('tagslogs')->where('sn', $more_sn['one'])->find();
			$more_sn = more_sn($info['sn']);
			$arr = strtoarr($more_sn['sn']);
		}
		$this->assign('arr', $arr);
		$this->assign('info', $info);
		return $this->fetch();
	}

	// 取消关联序列号1-1
	public function more_sn_del()
	{
		$status = 0;
		$msg = "操作失败";
		$data = $this->request->param();
		// 启动事务
		Db::startTrans();
		$post = $data;
		if ($this->is_card($post['sn'])) {
			// 回滚事务
			Db::rollback();
			return ['status' => 0, 'msg' => '已经是保修卡，不能解除关联'];
		}
		$more_sn = more_sn($post['sn']);


		if ($more_sn['status'] == 1) {
			$arr = strtoarr($more_sn['sn']);
			foreach ($arr as $key => $val) {
				if ($val == $post['delsn']) {
					unset($arr[$key]);
				}
			}
			$key = implode(',', $arr);
			$f = Db::name('more_sn')->where('id', $more_sn['id'])->update(['key' => $key]);
			//更换状态标识符
			$re1 = Db::name('tagslogs')->where('sn', $post['delsn'])->setField('snkey', 0);
			if ($f) {
				// 提交事务
				Db::commit();
				$status = 1;
				$msg = "操作成功";
			}
		}
		return ['status' => $status, 'msg' => $msg];
	}


	//编辑1-2
	public function moresnsave()
	{
		$status = 0;
		$msg = "操作失败";
		$data = $this->request->param();
		$post = $data;
		// 启动事务
		Db::startTrans();
		//执行更新操作
		if (!Db::name('tagslogs')->where('sn', $data['sn'])->find()) { //被关联者不存在的话
			// 回滚事务
			Db::rollback();
			return ['status' => $status, 'msg' => '无主序列号'];
		}



		if (!Db::name('tagslogs')->where('sn', $data['more_sn'])->find()) { //被关联者不存在的话
			//1.找到关联者
			$tg1 = Db::name('tagslogs')->where('sn', $data['sn'])->find();
			//2.找到关联着的同一批标签的空白标签
			$tg2 = Db::name('tagslogs')->where(['goodstags_id' => $tg1['goodstags_id'], 'sn' => ''])->find();
			if (empty($tg2)) {
				// 回滚事务
				Db::rollback();
				return ['status' => $status, 'msg' => '这批标签无剩余标签，请联系开发人员手动添加'];
			} else {
				$up = [
					'sn' => $data['more_sn'],
					'updatetime' => time(),
				];
				$tg3 = Db::name('tagslogs')->where(['id' => $tg2['id']])->update($up);
				if ($tg3) {
				} else {
					// 回滚事务
					Db::rollback();
					return ['status' => $status, 'msg' => '操作失败'];
				}
			}
		}
		$more_sn = Db::name('more_sn')->where('key', 'like', '%' . $data['sn'] . '%')->find();

		if ($more_sn) {
			$arr2 = Db::name('more_sn')->where('key', 'like', '%' . $data['more_sn'] . '%')->find();
			if ($arr2) {
				$key = $more_sn['key'] . ',' . $arr2['key'];
				Db::name('more_sn')->where('id', $arr2['id'])->delete();
			} else {
				$arr = explode(',', $more_sn['key']);
				array_push($arr, $data['more_sn']);
				$key = implode(',', $arr);
			}
			if ($this->is_card($data['more_sn'])) {
				// 回滚事务
				Db::rollback();
				$msg = '序列号已经添加了保修卡';
			} else {
				$res = Db::name('more_sn')->where('id', $more_sn['id'])->update(['key' => $key]);
			}
		} else {
			$key = $data['sn'] . ',' . $data['more_sn'];
			$arr2 = Db::name('more_sn')->where('key', 'like', '%' . $data['more_sn'] . '%')->find();
			if ($arr2) {
				$key = $data['sn'] . ',' . $arr2['key'];
				Db::name('more_sn')->where('id', $arr2['id'])->delete();
			}
			if ($this->is_card($data['more_sn'])) {
				// 回滚事务
				Db::rollback();
				$msg = '序列号已经添加了保修卡';
			} else {
				$res = Db::name('more_sn')->insert(['key' => $key]);
			}
		}

		if (isset($res) && $res) {
			$ar1 = more_sn($post['sn']); //更换状态标识符
			$re1 = Db::name('tagslogs')->where('sn', 'in', $ar1['sn'])->setField('snkey', 1);
			//统一保修卡
			$info = Db::name('card')->where('tagslogs_sn', $post['sn'])->find();
			if (!empty($info)) {
				$more = $info;
				unset($more['id']);
				$cstr = checkstr($ar1['sn'], ','); //判断是否是多个序列码-是否存在英文逗号
				if ($cstr) { //包含--多个序列码-包含英文逗号
					$sn_arr = strtoarr($ar1['sn']);
					foreach ($sn_arr as $ksn => $vsn) {
						if ($this->is_card($vsn)) {
							Db::name('card')->where('tagslogs_sn', $vsn)->delete();
						}
						$more['tagslogs_sn'] = $vsn;
						Db::name('card')->insert($more);
					}
				} else {
					if ($this->is_card($ar1['sn'])) {
						Db::name('card')->where('tagslogs_sn', $ar1['sn'])->delete();
					}
					$more['tagslogs_sn'] = $ar1['sn'];
					Db::name('card')->insert($more);
				}
			} else {

				$cstr = checkstr($ar1['sn'], ','); //判断是否是多个序列码-是否存在英文逗号
				if ($cstr) { //包含--多个序列码-包含英文逗号
					$sn_arr = strtoarr($ar1['sn']);
					foreach ($sn_arr as $ksn => $vsn) {
						if ($this->is_card($vsn)) {
							Db::name('card')->where('tagslogs_sn', $vsn)->delete();
						}
					}
				} else {
					if ($this->is_card($ar1['sn'])) {
						Db::name('card')->where('tagslogs_sn', $ar1['sn'])->delete();
					}
				}
			}







			//if($re1){
			// 提交事务
			Db::commit();
			$status = 1;
			$msg = "更新成功";
			adminlog('关联序列号:' . $key, '更新');

			// }else{
			// 	// 回滚事务
			//     Db::rollback();
			// }

		}
		return ['status' => $status, 'msg' => $msg];
	}

	private function is_card($sn)
	{
		$res = Db::name('card')->where('tagslogs_sn', $sn)->find();
		if ($res) {
			return true;
		}
		return false;
	}
}
